package app.fugema.shop.main.home.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import app.fugema.shop.main.order.model.AddressDetailResult;
import app.fugema.shop.main.order.model.AddressNewEntity;
import app.fugema.shop.main.order.model.AddressResult;

import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.UIUtils;
import com.base.common.viewmodel.BaseViewModel;

import java.util.HashMap;
import java.util.Map;

import app.fugema.shop.http.HttpService;

public class AddressVM extends BaseViewModel {

    public AddressVM(@NonNull Application application)
    {
        super(application);
    }

    /**
     *  添加 地址
     * @param adressAddInfo
     * @return
     */
    public MutableLiveData<LiveDataWrapper<BaseApiResult>> addAddr(AddressNewEntity adressAddInfo ,String userKey)
    {
        Map<String,Object> addrParams = new HashMap<>();
        if(UIUtils.isNotEmpty( adressAddInfo.getRegionId() ))
        {
            addrParams.put("regionId",adressAddInfo.getRegionId());
        }
        if(UIUtils.isNotEmpty( adressAddInfo.getAddress() ))
        {
            addrParams.put("address",adressAddInfo.getAddress());
        }
        if(UIUtils.isNotEmpty( adressAddInfo.getAddressDetail() ))
        {
            addrParams.put("addressDetail",adressAddInfo.getAddressDetail());
        }
        if(UIUtils.isNotEmpty( adressAddInfo.getPhone() ))
        {
            addrParams.put("phone",adressAddInfo.getPhone());
        }
        if(UIUtils.isNotEmpty( adressAddInfo.getShipTo() ))
        {
            addrParams.put("shipTo",adressAddInfo.getShipTo());
        }
        if(UIUtils.isNotEmpty( adressAddInfo.getLatitude() ))
        {
            addrParams.put("latitude",adressAddInfo.getLatitude());
        }
        if(UIUtils.isNotEmpty( adressAddInfo.getLongitude() ))
        {
            addrParams.put("longitude",adressAddInfo.getLongitude());
        }
        addrParams.put("userkey",userKey);

        return send(HttpService.getHttpServer().addAddress(addrParams));
    }

    /**
     *  删除 用户地址
     * @param addrId
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<BaseApiResult>> deleteAddress(int addrId,String userKey)
    {
        return send(HttpService.getHttpServer().deleteAddrById(addrId,userKey));
    }

    /**
     * 编辑 用户地址
     * @param addrId
     * @param adressAddInfo
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<BaseApiResult>> editAddress(int addrId,AddressNewEntity adressAddInfo ,String userKey)
    {
        Map<String,Object> addrParams = new HashMap<>();
        addrParams.put("id",addrId);
        if(UIUtils.isNotEmpty( adressAddInfo.getRegionId() ))
        {
            addrParams.put("regionId",adressAddInfo.getRegionId());
        }
        if(UIUtils.isNotEmpty( adressAddInfo.getAddress() ))
        {
            addrParams.put("address",adressAddInfo.getAddress());
        }
        if(UIUtils.isNotEmpty( adressAddInfo.getAddressDetail() ))
        {
            addrParams.put("addressDetail",adressAddInfo.getAddressDetail());
        }
        if(UIUtils.isNotEmpty( adressAddInfo.getPhone() ))
        {
            addrParams.put("phone",adressAddInfo.getPhone());
        }
        if(UIUtils.isNotEmpty( adressAddInfo.getShipTo() ))
        {
            addrParams.put("shipTo",adressAddInfo.getShipTo());
        }
        if(UIUtils.isNotEmpty( adressAddInfo.getLatitude() ))
        {
            addrParams.put("latitude",adressAddInfo.getLatitude());
        }
        if(UIUtils.isNotEmpty( adressAddInfo.getLongitude() ))
        {
            addrParams.put("longitude",adressAddInfo.getLongitude());
        }
        addrParams.put("userkey",userKey);
        return send(HttpService.getHttpServer().editAddress(addrParams));
    }

    /**
     *  获取 地址列表
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<AddressResult>> addressList(String userKey)
    {
        return send(HttpService.getHttpServer().getUserAddressList(userKey));
    }

    /**
     * 设置默认地址
     * @param addrId
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<BaseApiResult>> setDefAddr(int addrId,String userKey)
    {
        return send(HttpService.getHttpServer().setDefaultAddr(addrId,userKey));
    }

    /**
     *  获取 地址信息 详情
     * @param addrId
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<AddressDetailResult>> getAddressDetailById(int addrId, String userKey)
    {
        return send(HttpService.getHttpServer().getAddressDetailById(addrId,userKey));
    }
}
